package pl.charmas.parcelablegenerator.util;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import pl.charmas.parcelablegenerator.ClickGenerator;

/**
 * Created by zhangfy on 2015/9/17 根据布局中的setContentView等方式来查找R.laout.xx,生成Filed
 */
public class ViewAnnoationGenerator extends AnAction {
	public void actionPerformed(AnActionEvent e) {
		// TODO: insert action logic here
		final PsiClass psiClass = getPsiClassFromContext(e);
		final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiClass.getProject());
		//		elementFactory.createAnnotationType("@Ove");
		GlobalSearchScope scope = GlobalSearchScope.allScope(e.getProject());
		final PsiType psiTypeString = elementFactory.createTypeFromText(CommonClassNames.JAVA_LANG_STRING, null);
		//		PsiClass viewClass = JavaPsiFacade.getInstance(e.getProject()).findClass("android.widget.TextView", scope);
		JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(psiClass.getProject());

		a(psiClass,e.getProject());
	}

	public void a(final PsiClass nameTokensPsiClass, final Project project){


		new WriteCommandAction.Simple(nameTokensPsiClass.getProject(), nameTokensPsiClass.getContainingFile()) {
			@Override
			protected void run() throws Throwable {
				String fieldSource = "public static final String abc = \"abc\";";
				String methodSource = "public static String getAbc() {return abc;}";

				// add contents to class
				PsiElementFactory elementFactory = PsiElementFactory.SERVICE.getInstance(project);
				final PsiField newField = elementFactory.createFieldFromText(fieldSource, null);
				final PsiMethod newMethod = elementFactory.createMethodFromText(methodSource, null);

				JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project);
				styleManager.shortenClassReferences(nameTokensPsiClass.addBefore(newMethod, nameTokensPsiClass.getLastChild()));
				styleManager.shortenClassReferences(nameTokensPsiClass.addBefore(newField, nameTokensPsiClass.getLastChild()));
			}
		}.execute();


		/*ApplicationManager.getApplication().runWriteAction(new Runnable() {
			@Override
			public void run() {
//				nameTokensPsiClass.add(newField);
				nameTokensPsiClass.add(newMethod);
			}
		});*/
	}

	private PsiClass getPsiClassFromContext(AnActionEvent e) {
		PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);//得到文件psiJavaFile:A.java
		Editor editor = e.getData(PlatformDataKeys.EDITOR);

		if (psiFile == null || editor == null) {
			return null;
		}

		int offset = editor.getCaretModel().getOffset();
		PsiElement element = psiFile.findElementAt(offset);

		return PsiTreeUtil.getParentOfType(element, PsiClass.class);
	}


}
